<template>
    <div class="app-container calendar-list-container">

        <!-- 查询和其他操作 -->
        <div class="filter-container">
          <el-input clearable class="filter-item" style="width: 200px;" placeholder="商品编号" v-model="listQuery.goodsId"></el-input>
          <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">查找</el-button>
          <el-button class="filter-item" type="primary" @click="handleCreate" icon="el-icon-edit">添加</el-button>
        </div>

        <!-- 查询结果 -->
        <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>

          <el-table-column align="center" min-width="120px" label="平台编号" prop="platformId">
          </el-table-column>
          <el-table-column align="center" min-width="120px" label="商户账号" prop="merId">
          </el-table-column>
          <el-table-column align="center" min-width="120px" label="商品编号" prop="goodsId">
          </el-table-column>       
          <el-table-column align="center" min-width="100px" label="商品名称" prop="goodsName">
          </el-table-column>     

          <el-table-column align="center" min-width="200px" label="商品说明" prop="goodsDescription">
          </el-table-column>   
          <el-table-column align="center" min-width="200px" label="商品照片" prop="goodsImg">
          </el-table-column>   
          <el-table-column align="center" min-width="80px" label="起拍价" prop="startingPrice">
          </el-table-column>   
          <el-table-column align="center" min-width="80px" label="现价" prop="currentPrice" >
          </el-table-column>   
          <el-table-column align="center" min-width="120px" label="创建时间" prop="createTime" sortable>
          </el-table-column>   
          <el-table-column align="center" min-width="120px" label="截止时间" prop="deadlineTime">
          </el-table-column>   
          <el-table-column align="center" label="操作" width="250" >
              <template slot-scope="scope">
                <el-button type="danger" size="mini"  @click="handleDelete(scope.row)">删除</el-button>

                <el-button type="danger" size="mini"  @click="handleUpdate(scope.row)">修改</el-button>
              </template>
          </el-table-column> 

          
        </el-table>

        <!-- 分页 -->
        <div class="pagination-container">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page"
            :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        </div>

        <!-- 修改对话框 -->
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="addDialogFormVisible">
          <el-form :rules="rules" ref="dataForm" :model="dataForm" status-icon label-position="left" label-width="100px" style='width: 400px; margin-left:50px;'>
            <el-form-item label="平台编号" prop="platformId">
              <el-input v-model="dataForm.platformId" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="商品名称" prop="goodsName">
              <el-input v-model="dataForm.goodsName"></el-input>
            </el-form-item>  
            <el-form-item label="商品说明" prop="goodsDescription"> 
              <el-input v-model="dataForm.goodsDescription"></el-input>
            </el-form-item>          
            <el-form-item label="商品照片" prop="goodsImg">
               <div  @click="upLogo">
                <img v-if="dataForm.goodsImg" :src="dataForm.goodsImg">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </div>
              <input type="file" name="file" id="upload_logo" @change="uploadLogo" accept="image/png,image/gif" style="display: none">
            </el-form-item>         
            <el-form-item label="起拍价" prop="startingPrice"> 
              <el-input v-model="dataForm.startingPrice"></el-input>
            </el-form-item>  
            <el-form-item label="截止时间" prop="deadlineTime"> 
              <el-input v-model="dataForm.deadlineTime"></el-input>
            </el-form-item>  
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="addDialogFormVisible = false">取消</el-button>
            <el-button type="primary" v-if="this.dialogStatus == 'create'" @click="createData">确定</el-button>
            <el-button type="primary" v-if="this.dialogStatus == 'update'" @click="updateData">确定</el-button>
          </div>
        </el-dialog>
    </div>
</template>

<script>
import waves from '@/directive/waves' // 水波纹指令
import { listGoodsInfo, addGoodsInfo, deleteGoodsInfo, updateGoodsInfo } from '@/api/cms/auction/goodsInfo'
import { mapGetters } from 'vuex'
import { uploadImg } from '@/api/cms/society/index'

export default {
  name: 'auction',
  directives: {
    waves
  },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 20,
        goodsId: ''
      },
      list: [],
      total: 0,
      listLoading: false,
      addDialogFormVisible: false,
      downloadLoading: false,
      dialogStatus: '',
      textMap: {
        create: '创建',
        update: '编辑'
      },
      dataForm: {
        platformId: '',
        goodsName: '',
        goodsDescription: '',
        goodsImg: '',
        startingPrice: '',
        deadlineTime: ''
      },
      rules: {
        goodsName: [
          {
            required: true,
            message: '请输入商品名称',
            trigger: 'blur'
          }
        ],
        goodsImg: [
          {
            required: true,
            message: '请上传商品照片',
            trigger: 'blur'
          }
        ],
        startingPrice: [
          {
            required: true,
            message: '请输入商品名称',
            trigger: 'blur'
          },
          {
            pattern: /^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/,
            message: '输入格式错误，请重新输入!'
          }
        ],
        deadlineTime: [
          {
            required: true,
            message: '请输入截止时间',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'elements',
      'name',
      'platformId'
    ])
  },
  created() {
    this.getList()
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    getList() {
      this.listLoading = true
      listGoodsInfo(this.listQuery).then(response => {
        console.log(response)
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleCreate() {
      this.dataForm = {}
      this.dataForm.platformId = this.platformId
      this.dialogStatus = 'create'
      this.addDialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleUpdate(row) {
      this.dialogStatus = 'update'
      this.addDialogFormVisible = true
      this.$nextTick(() => {
        this.dataForm = row
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addGoodsInfo(this.dataForm).then(response => {
            this.getList()
            this.addDialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    upLogo: function() {
      document.getElementById('upload_logo').click()
    },
    uploadLogo(event) {
      if (event.target.files.length > 0) {
        this.file = event.target.files[0] // 提交的图片
        console.log(this.file)
        const param = new FormData() // 创建form对象
        param.append('file', this.file)
        uploadImg(param).then(response => {
          if (response.success) {
            this.dataForm.goodsImg = response.data.filePath
            alert('ok')
          }
        })
      }
    },

    updateData() {
      var startingPrice = this.dataForm.startingPrice
      this.dataForm.startingPrice = startingPrice.toString()
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateGoodsInfo(this.dataForm).then(response => {
            this.getList()
            this.addDialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },

    handleDelete(row) {
      this.$confirm('此操作将删除选中商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteGoodsInfo(row)
            .then(response => {
              console.log(response)
              if (response.success) {
                this.$notify({
                  title: '成功',
                  message: '操作成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              }
            })
        })
    }
  }
}
</script>

<style>

</style>


